package com.anche.cywebnew.Controller;

import com.anche.cywebnew.DAO.systemManager.OrganMapper;
import com.anche.cywebnew.DTO.CurrLoginInfo;
import com.anche.cywebnew.DTO.CurrLoginUserDTO;
import com.anche.cywebnew.Pojo.systemManager.SysUser;
import com.anche.cywebnew.Service.index.IndexService;
import com.anche.cywebnew.Service.systemManager.UserService;
import com.anche.cywebnew.Utils.result.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class IndexController {

    @Autowired
    private OrganMapper organMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private IndexService indexService;

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/")
    public String toHome() {
        int count = organMapper.selectAllCount();
        if (count > 0) {
            return "homePage/home";
        } else {
            return "homePage/initialize";
        }
    }

    @RequestMapping("/403")
    public String to403() {
        return "error/403";
    }

    @RequestMapping("/toConsole")
    public String toConsole() {
        return "homePage/console";
    }

    @RequestMapping("/quickAccessInfo")
    @ResponseBody
    public Object getQuickAccessInfo() {
        Map<String, Object> map = new HashMap<>();
        try {
            SysUser existUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            List<Map<String, String>> mapList = userService.getQuickAccess(existUser.getId());
            map.put("code", "1");
            map.put("simplePerm", mapList);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "0");
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @RequestMapping("/getEchartsData")
    @ResponseBody
    public Map<String, Object> getEchartsData() {
        Map<String, Object> map = new HashMap<>();
        List<Integer> cyywqs;
        List<Map<String, String>> ywlxfb;
        List<String> LastSevenDay;
        List<Integer> LastSevenBus;
        SysUser existUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        try {
            String pid = organMapper.selectPOrganByCyqxh(existUser.getOrgan());
            if (pid.equals("-1")) {
                cyywqs = indexService.getBussinessTrend("");
                ywlxfb = indexService.getBussinessSpread("");
                LastSevenBus = indexService.getLastSevenBus("");
            } else {
                cyywqs = indexService.getBussinessTrend(existUser.getOrgan());
                ywlxfb = indexService.getBussinessSpread(existUser.getOrgan());
                LastSevenBus = indexService.getLastSevenBus(existUser.getOrgan());
            }
            LastSevenDay = indexService.getLastSevenDay();
            map.put("code", "1");
            map.put("cyywqs", cyywqs);
            map.put("ywlxfb", ywlxfb);
            map.put("lastsevenday", LastSevenDay);
            map.put("lastsevenbus", LastSevenBus);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "0");
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @GetMapping("/currUserInfo")
    @ResponseBody
    public Object getCurrUserInfo() {
        try {
            CurrLoginInfo currLoginInfo = indexService.getCurrUserInfo();
            return ResultUtil.success(currLoginInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @GetMapping("/percentInfo")
    @ResponseBody
    public Object getPercentInfo() {
        Map<String, Object> map = new HashMap<>();
        float download;
        float success;
        float verify;
        float verifypass;
        SysUser existUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        try {
            String pid = organMapper.selectPOrganByCyqxh(existUser.getOrgan());
            if (pid.equals("-1")) {
                download = indexService.getDownloadPercent("");
                success = indexService.getSuccessPercent("");
            } else {
                download = indexService.getDownloadPercent(existUser.getOrgan());
                success = indexService.getSuccessPercent(existUser.getOrgan());
            }
            map.put("code", "1");
            map.put("download", download * 100);
            map.put("success", success * 100);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "0");
            map.put("msg", e.getMessage());
        }
        return map;
    }
}
